<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/16
 * Time: 11:41
 */

namespace app\shop\admin;

use app\common\builder\ZBuilder;



class Demo
{
    public function index()
    {
        set_time_limit(0);
        $goods_id = input('get.id',0);
        $res = db('goods')->limit(2,50)->order('id desc')->select();
        foreach ($res as $k=>$v){
            $goods_id = $v['id'];
            $goods_name = $v['goods_name'];
//            $dir = iconv("UTF-8", "GBK", "goods/".$goods_id);
            $list = db('goods_gallery')->where('goods_id',$goods_id)->column('img_url');

//            if (file_exists($dir)){
//                continue;
//            }

//            if(!mkdir ($dir,0777,true)){
//                    continue;
//                }
                if(mb_strlen($v['thumb_url']) <5){
                echo count($v['thumb_url']);
                    continue;
                }
                $this->getImage($v['thumb_url'],'goods/',$goods_id);
                foreach ($list as $k2=>$v2){
                    $this->getImage($v2,'goods/',$goods_id);
                }

        }

    }

    public function getImage($url,$save_dir='',$filename='',$type=0){
        if(trim($url)==''){
            return array('file_name'=>'','save_path'=>'','error'=>1);
        }
        if(trim($save_dir)==''){
            $save_dir='./';
        }
        $ext=strrchr($url,'.');
        $filename = $filename.'__'.time().$ext;
        if(trim($filename)==''){//保存文件名

            if($ext!='.gif'&&$ext!='.jpg'&&$ext!='.png'){
                return array('file_name'=>'','save_path'=>'','error'=>3);
            }
            $filename=time().$ext;
        }
        if(0!==strrpos($save_dir,'/')){
            $save_dir.='/';
        }
        //创建保存目录
//        $dir = iconv("UTF-8", "GBK", $save_dir);
//        if(!file_exists($save_dir)&&!mkdir($dir,0777,true)){
//            return array('file_name'=>'','save_path'=>'','error'=>5);
//        }
        //获取远程文件所采用的方法
        if($type){
            $ch=curl_init();
            $timeout=5;
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
            $img=curl_exec($ch);
            curl_close($ch);
        }else{
            ob_start();
            readfile($url);

            $img=ob_get_contents();
            ob_end_clean();
        }
        //$size=strlen($img);
        //文件大小
        $fp2=@fopen($save_dir.$filename,'a');
        dump($fp2);
        fwrite($fp2,$img);
        fclose($fp2);
        unset($img,$url);
        return array('file_name'=>$filename,'save_path'=>$save_dir.$filename,'error'=>0);
    }

    public function down_images($url)
    {


        $header = array("Connection: Keep-Alive", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Pragma: no-cache", "Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3", "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:29.0) Gecko/20100101 Firefox/29.0");

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);

        //curl_setopt($ch, CURLOPT_HEADER, $v);

        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');

        $content = curl_exec($ch);

        $curlinfo = curl_getinfo($ch);

        //print_r($curlinfo);

        //关闭连接

        curl_close($ch);

        dump($curlinfo);die;
        if ($curlinfo['http_code'] == 200) {

            if ($curlinfo['content_type'] == 'image/jpeg') {

                $exf = '.jpg';

            } else if ($curlinfo['content_type'] == 'image/png') {

                $exf = '.png';

            } else if ($curlinfo['content_type'] == 'image/gif') {

                $exf = '.gif';

            }

            //存放图片的路径及图片名称  *****这里注意 你的文件夹是否有创建文件的权限 chomd -R 777 mywenjian

            $filename = date("YmdHis") . uniqid() . $exf;//这里默认是当前文件夹，可以加路径的 可以改为$filepath = '../'.$filename
            $filepath = 'images/' . $filename;
            var_dump($content);
            $res = file_put_contents($filepath, $content);
            //$res = file_put_contents($filename, $content);//同样这里就可以改为$res = file_put_contents($filepath, $content);
            //echo $filepath;
            echo $res;
        }
    }
}